<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar" fixed>
      <div class="logo" slot="left"></div>

      <van-button
        class="search-btn"
        slot="right"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>

    <!-- 频道列表 -->
    <!-- animated：切换动画 -->
    <!-- swipeable：手势滑动切换 -->
    <van-tabs v-model="active" class="channel-tabs " animated swipeable>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :id="item.id" :refname="'artlist' + item.id" />
      </van-tab>

      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="popupEditFn">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 频道编辑 -->
    <van-popup
      class="edit-channel-popup"
      v-model="showEditPop"
      closeable
      round
      position="bottom"
      :style="{ height: '90%' }"
    >
      <channel-edit
        :active="active"
        :mychannels="channels"
        @update-active="onUpdateActive"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannelsAPI } from '@/api'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'

import ArticleList from '@/components/ArticleList.vue'
import channelEdit from '../../components/channel-edit.vue'
export default {
  name: 'HomePage',
  components: {
    ArticleList,
    channelEdit
  },

  data () {
    return {
      active: 0,
      channels: [],
      showEditPop: false, // 频道编辑弹出层
      allChannels: []
    }
  },
  computed: {
    ...mapState(['user', 'channelsKey'])
  },

  created () {
    // 3. 调用获取频道列表
    this.loadChannels()
  },
  methods: {
    // 2. 定义加载频道列表数据的方法
    async loadChannels () {
      try {
        let channles = []
        const localChannels = getItem(this.channelsKey)
        if (this.user || !localChannels) {
          // 登录 或者 本地没有存储 获取后端数据
          const { data } = await getUserChannelsAPI()
          this.channels = data.data.channels
          return false
        } else {
          // 未登录并且本地没有数据
          channles = localChannels
        }
        this.channels = channles
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    popupEditFn () {
      this.showEditPop = true
    },
    onUpdateActive (ind, flag) {
      this.active = ind
      this.showEditPop = flag
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  padding-top: 180px;

  .logo {
    width: 200px;
    height: 100%;
    background: url('~@/assets/logo.png') no-repeat left;
    background-size: 100%;
  }
  /deep/ .van-nav-bar__right {
    max-width: unset; // 属性取消

    .search-btn {
      width: 400px;
      height: 64px;
      background-color: #55a7ff;
      border: none;
      font-size: 28px;
      .van-icon {
        font-size: 32px;
      }
    }
  }

  // 2.tab栏 ---------------------------------
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }

    .van-tabs__wrap {
      position: fixed;
      left: 0;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .placeholder {
      //元素不参与flex 布局的宽度分配
      flex-shrink: 0;
      width: 100px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.iconfont {
        font-size: 33px;
      }
      &::before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }

  .edit-channel-popup {
    padding-top: 100px;
    box-sizing: border-box;
  }
}
</style>
